package com.able.wanandroid.module.knowledge;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.able.wanandroid.R;
import com.able.wanandroid.base.BaseActivity;
import com.able.wanandroid.databinding.ActivityKnowledgeListBinding;
import com.able.wanandroid.utils.GsonUtils;
import com.able.wanandroid.utils.LogUtils;
import com.able.wanandroid.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;
import java.util.Map;

public class KnowledgeListActivity extends BaseActivity<ActivityKnowledgeListBinding> {

    private List<Map<String, Object>> mKnowledgeList;
    private KnowledgeAdapter mKnowledgeAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_knowledge_list);

        initView();
        initData();
        initAdapter();
    }

    private void initView() {
        setTitle("知识列表");
        setNavigationIcon(R.drawable.ic_arrow_back);
        showEmpty();
    }

    private void initData() {
//        mKnowledgeList = new ArrayList<>();
//        mKnowledgeList.add("兼容8.0 notification");
//        mKnowledgeList.add("DownloadManager");
//        mKnowledgeList.add("自定义键盘");

        String knowledgeStr = GsonUtils.getJsonFromAssets(this, "knowledge.json");
        mKnowledgeList = GsonUtils.getListMap(knowledgeStr);
    }

    private void initAdapter() {
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mKnowledgeAdapter = new KnowledgeAdapter(R.layout.item_knowledge_list, mKnowledgeList);
        mKnowledgeAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mBinding.recyclerView.setAdapter(mKnowledgeAdapter);

        mKnowledgeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String className = (String) mKnowledgeList.get(position).get("className");

//                dealClick(className);
                try {
                    gotoActivity(Class.forName("com.able.wanandroid.module.knowledge." + className));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    LogUtils.e(e.getException());
                    ToastUtils.showError(KnowledgeListActivity.this, e.getMessage());
                }
            }
        });
    }

    private void dealClick(String className) {
        switch (className) {
            case "CarNoActivity":
                gotoActivity(CarNoActivity.class);
                break;
        }
    }

    private void gotoActivity(Class<?> cls) {
        Intent intent = new Intent(KnowledgeListActivity.this, cls);
        startActivity(intent);
    }


}